/// @tags:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

namespace BlueQuantum {

int const N = 1e3 + 5, INF = 0x3f3f3f3f;

int n;
int a[N], f[N][N][2];

inline int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  memset(f, INF, sizeof(f));
  f[1][0][0] = f[1][0][1] = 0;
  for (int i = 2; i <= n; ++i) {
    f[i][0][0] = f[i - 1][0][0] + abs(a[i] - a[i - 1]);
    f[i][0][1] = f[i - 1][0][1] + abs(a[i] - a[i - 1]);
    f[i][i - 1][0] = min(f[i][i - 1][0], f[i - 1][0][1]);
    f[i][i - 1][1] = min(f[i][i - 1][1], f[i - 1][0][0]);
    for (int j = 1; j < i - 1; ++j) {
      f[i][j][0] = min(f[i][j][0], f[i - 1][j][0] + abs(a[i] - a[i - 1]));
      f[i][j][1] = min(f[i][j][1], f[i - 1][j][1] + abs(a[i] - a[i - 1]));
      f[i][i - 1][0] = min(f[i][i - 1][0], f[i - 1][j][1] + abs(a[i] - a[j]));
      f[i][i - 1][1] = min(f[i][i - 1][1], f[i - 1][j][0] + abs(a[i] - a[j]));
    }
  }
  int ans = INF;
  for (int j = 0; j < n; ++j) ans = min(ans, min(f[n][j][0], f[n][j][1]) + abs(a[n] - a[j]));
  cout << ans;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("force.in", "r", stdin);
  freopen("force.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
